package com.cogent.common.util;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

public class FormUtil {
	
	public Map getFormInfo(HttpServletRequest request){
		Map map = new HashMap();
		try {
			request.setCharacterEncoding("utf-8");
		} catch(Exception e) {       
		}
		Enumeration para =request.getParameterNames();
		String name=null;	        
		while (para.hasMoreElements()) {
		    name=(String)para.nextElement();
		    if(request.getParameter(name) != null) {
		        String value = request.getParameter(name).trim();
		        map.put(name, value);              
		    } else {
		    	map.put(name, "");             
		    }
		}	    
		
		return map;
	}
	
	public static HashMap getFormInfoNew(HttpServletRequest request){
		String method = request.getMethod().toLowerCase();
		HashMap map = new HashMap();
		try {
			request.setCharacterEncoding("utf-8");
		} catch(Exception e) {       
		}
		Enumeration para =request.getParameterNames();
		String name=null;	        
		while (para.hasMoreElements()) {
		    name=(String)para.nextElement();
		    if(request.getParameter(name) != null) {
		        String value = "";
		        if(method.equals("get"))
		        	value = getStringToUTF8(request, name);
		        else {
		        	String[] temp = request.getParameterValues(name);
		        	value = getValue(temp);
		        }
		        if(value.length() > 0)
		        	map.put(name.toLowerCase(), value); 
		        else
		        	map.put(name.toLowerCase(), null);         
		    } else
		    	map.put(name.toLowerCase(), null); 
		}	    
		
		return map;
	}
	
	private static String getValue(String[] temp) {
		String ret = "";
		if(temp.length > 0) {
			for(int i=0; i<temp.length; i++)
				ret += temp[i].trim() + ",";
			ret = ret.substring(0, ret.length()-1);
		}
		
		return ret;
	}
	
	public static String getStringToUTF8(HttpServletRequest request, String parameter) {
		String str = request.getParameter(parameter);
		if (str == null || str.trim().equals(""))
			return "";
		else {
			str = str.trim();
			String webApp = Globals.getProperty("webapp");
			if (webApp == null || webApp.trim().equals("")) {
				try {
					str = new String(str.getBytes("ISO8859_1"), "UTF-8");
				} catch (Exception e) {
					throw new RuntimeException(e);
				}
			}
			
			return str;
		}
	}
	
	public static HashMap getAjaxFormInfo(HttpServletRequest request){
		HashMap map = new HashMap();
		try {
			request.setCharacterEncoding("utf-8");
		} catch(Exception e) {       
		}
		Enumeration para =request.getParameterNames();
		String name=null;	        
		while (para.hasMoreElements()) {
		    name=(String)para.nextElement();
		    if(request.getParameter(name) != null) {
		        String value = unescape(request.getParameter(name));
		        if(value.length() > 0)
		        	map.put(name.toLowerCase(), value); 
		        else
		        	map.put(name.toLowerCase(), null);         
		    } else
		    	map.put(name.toLowerCase(), null); 
		}	    
		
		return map;
	}	
	
	private static String unescape(String src) {
		StringBuffer tmp = new StringBuffer();
		tmp.ensureCapacity(src.length());
		int lastPos = 0, pos = 0;
		char ch;
		while (lastPos < src.length()) {
			pos = src.indexOf("%", lastPos);
			if (pos == lastPos) {
				if (src.charAt(pos + 1) == 'u') {
					ch = (char) Integer.parseInt(src.substring(pos + 2, pos + 6), 16);
					tmp.append(ch);
					lastPos = pos + 6;
				} else {
					ch = (char) Integer.parseInt(src.substring(pos + 1, pos + 3), 16);
					tmp.append(ch);
					lastPos = pos + 3;
				}
			} else {
				if (pos == -1) {
					tmp.append(src.substring(lastPos));
					lastPos = src.length();
				} else {
					tmp.append(src.substring(lastPos, pos));
					lastPos = pos;
				}
			}
		}
		
		return tmp.toString();
	}
}
